package hk.polyu.rotary.dial;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.view.MotionEvent;

public class RotaryDialActivity extends Activity {
	/** Called when the activity is first created. */
	float ring_w, ring_h;
	float view_w;
	float view_h;
	float d_angle;
	ImageView DR;
	Matrix mtx;
	Matrix matrix;
	
	private Bitmap ringBM;
	boolean matrixSet = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		DR = (ImageView) findViewById(R.id.dialring);
		// BitmapFactory.Options opts = new BitmapFactory.Options();
		// opts.inJustDecodeBounds = true;
		// BitmapFactory.decodeResource(getResources(),R.drawable.rotary_ring,
		// opts);
		// opts.inSampleSize = computeSampleSize(opts, -1, 480*480);
		// /opts.inJustDecodeBounds = false;

		ringBM = BitmapFactory.decodeResource(getResources(),
				R.drawable.rotary_ring);

		ring_w = ringBM.getWidth();
		ring_h = ringBM.getHeight();
		DR.setImageBitmap(ringBM);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		view_w= dm.widthPixels;
		view_h = dm.heightPixels;
		matrix = new Matrix();
		matrix.setScale(view_w / ring_w, view_w / ring_w);
		DR.setImageMatrix(matrix);
		DR.setScaleType(ScaleType.MATRIX);
		DR.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				//if(!matrixSet
				// {
				// mtx = DR.getImageMatrix();
				// matrixSet = true;
				// }

				//view_w = DR.getWidth();
				//view_h = DR.getHeight();
				float rX = event.getX() - ring_w / 2;
				float rY = event.getY() - ring_h / 2;
//				float rX = event.getRawX() - ring_w / 2;
//				float rY = event.getRawY() - ring_h / 2;
				Double angle = Math.atan2(rX, rY);
				//d_angle = (float) (angle * (180 / Math.PI));
				d_angle = (float)(angle*1);
				Log.d("current RAW", event.getRawX() + "," + event.getRawY());
				Log.d("Current ringw",ring_w+ "," + ring_h);
				Log.d("current point", rX + "," + rY);
				Log.d("angle", "" + d_angle);

				matrix = new Matrix();
				
				float val[] = new float[9];
				Matrix drmatrix = new Matrix();
				
				drmatrix.getValues(val);
				
				float r00 = (float) Math.cos(-d_angle);
				float r01 = (float) -Math.sin(-d_angle);
				float r10 = (float) Math.sin(-d_angle);
				float r11 = (float) Math.cos(-d_angle);
				float x = ring_w/2;
				float y = ring_h/2;
				val[0] = r00;
				val[1] = r01;
				val[2] = x - r00*x -r01*y;
				val[3] = r10;
				val[4] = r11;
				val[5] = y - r10*x -r11*y;
				val[6] = 0;
				val[7] = 0;
				val[8] = 1;
				drmatrix.setValues(val);
				//drmatrix.postTranslate(9,9 );
				
				
				drmatrix.postScale(view_w / ring_w, view_w / ring_w);
				DR.setImageMatrix(drmatrix);
				//matrix.postRotate(d_angle,ring_w,ring_h);
		//		matrix.postTranslate(view_w/2,view_h/2);

				// matrix;
				// matrix.postScale(scale, scale);
				
				// BitmapFactory.Options opts = new BitmapFactory.Options();
				// opts.inJustDecodeBounds = true;

				// Bitmap.createBitmap(ringBM, 0, 0, (int)ring_w, (int)ring_h,
				// matrix,
				// true);
				// opts.inSampleSize = computeSampleSize(opts, -1, 128*128);
				// opts.inJustDecodeBounds = false;

				// Bitmap rotatedBM = Bitmap.createBitmap(ringBM, 0, 0,
				// (int)ring_w,
				// (int)ring_h, matrix, true);
				//DR.setImageMatrix(matrix);
//				float values[] = new float[9];
//				matrix.getValues(values);
//				for (float on9 : values) {
//					Log.d("debug", "value :" + on9);
//				}

				// DR.setImageBitmap(rotatedBM);

				return true;

			}
		});

	}

	// @Override
	// public void onContentChanged(){
	// //DR = (ImageView)findViewById(R.id.dialring);
	// // DR.setImageResource(R.drawable.rotary_ring);
	//
	// //
	//
	// //DR.setImageBitmap(ringBM);
	//
	//
	//
	// }

	public static int computeSampleSize(BitmapFactory.Options options,
			int minSideLength, int maxNumOfPixels) {
		int initialSize = computeInitialSampleSize(options, minSideLength,
				maxNumOfPixels);

		int roundedSize;
		if (initialSize <= 8) {
			roundedSize = 1;
			while (roundedSize < initialSize) {
				roundedSize <<= 1;
			}
		} else {
			roundedSize = (initialSize + 7) / 8 * 8;
		}

		return roundedSize;
	}

	private static int computeInitialSampleSize(BitmapFactory.Options options,
			int minSideLength, int maxNumOfPixels) {
		double w = options.outWidth;
		double h = options.outHeight;

		int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
				.sqrt(w * h / maxNumOfPixels));
		int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
				Math.floor(w / minSideLength), Math.floor(h / minSideLength));

		if (upperBound < lowerBound) {
			// return the larger one when there is no overlapping zone.
			return lowerBound;
		}

		if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
			return 1;
		} else if (minSideLength == -1) {
			return lowerBound;
		} else {
			return upperBound;
		}
	}

	private void loadImage() {

	}

}